export default class ScrollContainer {
    constructor(containerSelector) {
        let container = window;

        if (containerSelector) {
            container =
                containerSelector == ""
                    ? document.querySelector(containerSelector) || window
                    : containerSelector;
        }

        this.container = container;
    }

    addScrollListener(callback) {
        this.scrollListenerCallback = callback;
        this.container.addEventListener("scroll", callback);
    }

    removeScrollListener() {
        this.container.removeEventListener(
            "scroll",
            this.scrollListenerCallback
        );
    }

    getScrollTop() {
        return this.container.scrollTop;
    }
    getDistanceFromTop() {
        let yPosition = 0;
        let nextElement = this.container;

        while (nextElement) {
            yPosition += nextElement.offsetTop;
            nextElement = nextElement.offsetParent;
        }

        return yPosition;
    }

    scrollTo(x, y) {
        return this.container.scrollTo(x, y);
    }

    scrollToWithMotion (x, y) {
        return this.container.scrollTo({
            left: x,
            top: y,
            behavior: "smooth",
            duration: 500,
        });
    }

    getOffsetTop() {
        return this.container.offsetTop;
    }
}
